package bidgetserver;

import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//email Module:
import emailModule.emailSender;


public class UpdateSearchReminder extends HttpServlet{
	private static final Logger log = Logger.getLogger(UpdateSearchReminder.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {

		emailSender.SendEmail("dephrath@gmail.com","mail_1: ", "first line");
		String searchString = req.getParameter("SearchString");
		String userName = req.getParameter("UserName");
		String reminderID = req.getParameter("reminderID");
		String reminderPic= req.getParameter("reminderPic"); 
		String Content = "search string:"+searchString+"+user Name:"+userName+"reminder id:"+reminderID+"reminder pic:"+reminderPic;
		emailSender.SendEmail("dephrath@gmail.com","mail_2: ", Content);

		PersistenceManager pm = PMF.get().getPersistenceManager();

		String query = "select from "+Reminder.class.getName()+" where userName == '"+userName+"'";
		query+=" && searchString == '"+searchString+"'";

		List<Reminder> reminderToUpdate = (List<Reminder>) pm.newQuery(query).execute();
		if(reminderToUpdate.isEmpty()){
			emailSender.SendEmail("dephrath@gmail.com","mail_2: ", "empty");
		}
		else{
			emailSender.SendEmail("dephrath@gmail.com","mail_2: ", "not empty");
		}
		//if we made it here it means exactly 1 reminder with the above parameters was found.
		
		//MAIL BEGIN
		//mail sending to client:
		String MsgBodyString = "Dear <B>" +userName+"</B>,"+"<br><br>";
		MsgBodyString+="A new search result for <B>"+"\""+searchString+"\""+"</B> just popped up in eBay."+"<br>";
		MsgBodyString+="To view the new auction available please check out your Bidget bidding widget."+"<br><br>";
		MsgBodyString+="Thank you,"+"<br>";
		MsgBodyString+="Bidget Team.";
		
		if(!(reminderPic.equals(null)) && !(reminderPic.equals(""))){

			MsgBodyString+="<br><br><B>Product picture bellow:</B><br>";
			String imgStr = "<img src='"+reminderPic+"' align='middle'><br><br>";
			MsgBodyString+=imgStr;
			//MsgBodyString+=reminderPicStr;
			
		}
		
		String BidGetLogoPicStr="<img src='http://img199.imageshack.us/img199/5971/bidgetlogo3.png'>";
		String ContactUsStr="Contact us at:<br><B>auction.notifier@gmail.com</B>";
		
		MsgBodyString+="<table><tr></tr><td>____________________________________________________________</td><tr><td>"+BidGetLogoPicStr+"</td></tr><tr><td>";
		MsgBodyString+=ContactUsStr;
		MsgBodyString+="</td></tr></table>";
		
		emailSender.SendEmail(userName, 
				"A new search result just popped up - BidGet automatic notification", MsgBodyString);
		
		//MAIL END

		//updating the reminder ID:

		//make new reminder with the new reminder ID:
		Reminder reminder = new Reminder(reminderToUpdate.get(0).getSearchString(),
				reminderToUpdate.get(0).getUserName(),
												     reminderToUpdate.get(0).getMinPrice(),
												     reminderToUpdate.get(0).getMaxPrice(),
												     reminderToUpdate.get(0).getTop5Results()
				                                     );
		reminder.setReminderID(reminderID);


		try{
			//delete the old reminder:
			//pm.deletePersistent(reminderToUpdate.get(0));
			
			//make the new reminder persistent:
			pm.makePersistent(reminder);
		}
		finally{
			pm.close();
		}

	}
}
